package com.gzc.service.imp;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gzc.common.Pager;
import com.gzc.common.SystemContext;
import com.gzc.dao.IAttachmentDao;
import com.gzc.dao.IDepartmentDao;
import com.gzc.dao.IDocumentDao;
import com.gzc.dao.IUserDao;
import com.gzc.dto.AttachDto;
import com.gzc.model.Attachment;
import com.gzc.model.DepDocument;
import com.gzc.model.Department;
import com.gzc.model.Document;
import com.gzc.model.User;
import com.gzc.model.UserReadDocument;
import com.gzc.service.IDocumentService;

@Service("documentService")
public class DocumentService implements IDocumentService {

	private IDocumentDao documentDao;
	private IUserDao userDao;
	private IAttachmentDao attachmentDao;
	private IDepartmentDao departmentDao;

	@Override
	public void add(Document doc, Integer[] depIds, AttachDto ad)
			throws IOException {
		doc.setCreateDate(new Date());
		doc.setUser(SystemContext.getLoginUser());
		documentDao.add(doc);

		List<Department> deps = departmentDao.listByIds(depIds);
		DepDocument dd = null;
		for (Department dep : deps) {
			dd = new DepDocument();
			dd.setDep(dep);
			dd.setDocument(doc);
			documentDao.addObj(dd);
		}
		// DocumentUtil.addAttach(ad, attachmentDao, null, doc);
	}

	@Override
	public void delete(int id) {
		// TODO 自动生成的方法存根

	}

	@Override
	public Document updateRead(int id, Integer isRead) {
		User u = SystemContext.getLoginUser();
		Document d = documentDao.load(id);
		if (isRead == null || isRead == 0) {
			if (!checkDocIsRead(u.getId(), id)) {
				// 将该文档添加为已读
				UserReadDocument urd = new UserReadDocument();
				urd.setUser(u);
				urd.setDocument(d);
				documentDao.addObj(urd);
			}
		}
		return d;
	}

	private boolean checkDocIsRead(int userId, int docId) {
		String hql = "select count(*) from UserReadDocument urd where urd.user.id=? and urd.document.id=?";
		Long count = (Long) documentDao.queryByHql(hql, new Object[] { userId,
				docId });
		if (count == null || count == 0)
			return false;
		return true;
	}

	@Override
	public Pager<Document> findSendDocument(int userId) {
		String hql = "select new Document(id,title,content,createDate) from Document doc "
				+ "where doc.user.id=? order by createDate desc";
		return documentDao.find(hql, userId);
	}

	@Override
	public Pager<Document> findReadDocument(String con, Integer depId) {
		User u = SystemContext.getLoginUser();
		String hql = "select doc from Document doc left join fetch doc.user u left join fetch u.department dep "
				+ "where doc.id in (select urd.document.id from UserReadDocument urd where urd.user.id=?)";

		if (con != null && !"".equals(con)) {
			hql += " and (doc.title like '%" + con
					+ "%' or doc.content like '%" + con + "%')";
		}
		if (depId != null && depId > 0) {
			hql += " and dep.id=" + depId;
		}
		hql += " order by doc.createDate desc";
		return documentDao.find(hql, u.getId());
	}

	@Override
	public Pager<Document> findNotReadDocument(String con, Integer depId) {
		User u = SystemContext.getLoginUser();
		String hql = "select doc from Document doc left join fetch doc.user u left join fetch u.department dep where "
				+ "doc.id not in (select urd.document.id from UserReadDocument urd where urd.user.id=?)";

		if (con != null && !"".equals(con)) {
			hql += " and (doc.title like '%" + con
					+ "%' or doc.content like '%" + con + "%')";
		}
		if (depId != null && depId > 0) {
			hql += " and dep.id=" + depId;
		}
		return documentDao.find(hql, u.getId());
	}

	@Override
	public List<Attachment> listAttachmentByDocument(int docId) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public Document load(int id) {
		return documentDao.load(id);
	}

	@Override
	public List<Department> listDocSendDep(int id) {
		String hql = "select dd.dep from DepDocument dd where dd.document.id=?";
		return departmentDao.list(hql, id);
	}

	// ==============================================

	@Resource
	public void setDocumentDao(IDocumentDao documentDao) {
		this.documentDao = documentDao;
	}

	@Resource
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Resource
	public void setAttachmentDao(IAttachmentDao attachmentDao) {
		this.attachmentDao = attachmentDao;
	}

	@Resource
	public void setDepartmentDao(IDepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

}
